package cn.tedu.project.oa.attendanceRule.service.impl;

import cn.tedu.project.oa.attendanceRule.mapper.WorkingShiftMapper;
import cn.tedu.project.oa.attendanceRule.pojo.entity.WorkingShift;
import cn.tedu.project.oa.attendanceRule.pojo.param.WorkingShiftSelectParam;
import cn.tedu.project.oa.attendanceRule.pojo.vo.WorkingShiftVO;
import cn.tedu.project.oa.attendanceRule.service.IWorkingShiftService;
import cn.tedu.project.oa.dingding.DingDingConstant;
import cn.tedu.project.oa.dingding.DingDingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class WorkingShiftServiceImpl implements IWorkingShiftService {
    @Autowired
    private WorkingShiftMapper workingShiftMapper;

    /**
     * 查询班次
     *
     * @param id 班次主键
     * @return 班次
     */
    @Override
    public WorkingShift selectWorkingShiftById(Long id)
    {
        return workingShiftMapper.selectById(id);
    }

    /**
     * 查询班次列表
     */
    @Override
    public List<WorkingShiftVO> selectWorkingShiftList(WorkingShiftSelectParam workingShiftSelectParam)
    {
        QueryWrapper<WorkingShift> queryWrapper = new QueryWrapper();
        if (workingShiftSelectParam.getName() != null){
            queryWrapper.like("name", workingShiftSelectParam.getName());
        }else if (workingShiftSelectParam.getId() != null){
            queryWrapper.eq("id", workingShiftSelectParam.getId());
        }
        List<WorkingShift> list = workingShiftMapper.selectList(queryWrapper);
        List<WorkingShiftVO> workingShiftVOList = new java.util.ArrayList<>();
        for (WorkingShift workingShift : list){
            WorkingShiftVO workingShiftVO = new WorkingShiftVO();
            workingShiftVO.setId(workingShift.getId());
            workingShiftVO.setName(workingShift.getName());
            workingShiftVO.setCheckInTime(workingShift.getCheckInTime());
            workingShiftVO.setCheckOutTime(workingShift.getCheckOutTime());
            workingShiftVOList.add(workingShiftVO);
        }
        return workingShiftVOList;

    }

    /**
     * 新增班次
     *
     * @param workingShift 班次
     * @return 结果
     */
    @Override
    public int insertWorkingShift(WorkingShift workingShift) {
        Long id = null;
        try {
            id = DingDingService.addShift(workingShift);
        } catch (ApiException e) {
            //todo 处理新增异常
            throw new RuntimeException(e);
        }
        workingShift.setId(id);
        workingShift.setCreateTime(new Date());
        log.debug("新增班次：" + workingShift);
        return workingShiftMapper.insert(workingShift);
    }


    /**
     * 删除班次信息
     *
     * @param id 班次主键
     * @return 结果
     */
    @Override
    public int deleteWorkingShiftById(Long id)
    {
        try {
            DingDingService.deleteShiftById(id);
        } catch (ApiException e) {
            //todo 处理删除班次异常
            throw new RuntimeException(e);
        }
        return workingShiftMapper.deleteById(id);
    }







}
